package ypmits.components.listbox.view.item {
	import ypmits.bitmap.BitmapModifierTool;
	import ypmits.effects.FilterEffects;
	import ypmits.utils.SimpleMath;

	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	/**
	 * @author Dave Lenz
	 */
	public class ListBoxItem extends Sprite implements IListBoxItem {

		private var _items : int = 0;
		private var _width : Number;
		private var _data : ListBoxItemData;
		private var _textfield : TextField;
		private var _itemgraphic : Sprite;
		private var _itemtf : TextField;
		private var _button : Sprite;
		private var _preloaderbarbelow : Sprite;
		private var _preloaderbar : Sprite;
		private var percentage : Number;
		private var bitmap : Bitmap;

		
		
		public function ListBoxItem( width : Number, data : ListBoxItemData ) {
			_width = width;
			_data = data;
			
			draw( );
		}

		
		override public function get height() : Number {
			var r : Rectangle = _textfield.getRect( this );
			return r.height + 2;
		}

		
		public function setPercentageLoaded( perc : Number ) : void {
			this.percentage = perc;
			_preloaderbar.scaleX = perc / 100;
			endHandler();
		}
		
		
		public function endHandler(  ) : void {
			if( percentage ) stopLoading();
		}

		
		public function startLoading() : void {
			_preloaderbar.alpha = 1;
			_preloaderbar.visible = true;
			_preloaderbarbelow.alpha = 1;
			_preloaderbarbelow.visible = true;
		}

		
		private function stopLoading() : void {
			_preloaderbar.alpha = 0;
			_preloaderbar.visible = false;
			_preloaderbarbelow.alpha = 0;
			_preloaderbarbelow.visible = false;
		}

		
		private function draw() : void {
			var format : TextFormat = new TextFormat( "PF Ronda Seven", 8, 0x000000, null, null, null, null, null, TextFieldAutoSize.LEFT );

			_textfield = new TextField( );
			_textfield.autoSize = TextFieldAutoSize.LEFT;
			_textfield.embedFonts = true;
			_textfield.selectable = false;
			_textfield.text = _data.label;
			_textfield.setTextFormat( format );
			_textfield.defaultTextFormat = format;
			_textfield.multiline = false;
			_textfield.wordWrap = false;
			_textfield.width = _width;
			_textfield.border = false;
			_textfield.x = 7;
			_textfield.mouseEnabled = false;
			addChild( _textfield );
			
			var r : Rectangle = _textfield.getRect( this );
			
			var headmatrix : Matrix = new Matrix( );
			headmatrix.createGradientBox( r.width, r.height, SimpleMath.degreesToRadians( 90 ) );
			
			_button = new Sprite( );
			_button.graphics.clear( );
			_button.graphics.beginGradientFill( GradientType.LINEAR, [ 0xE6E6E6, 0xDADADA ], [ 1, 1 ], [ 0, 0xFF ], headmatrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0 );
			_button.graphics.drawRect( 0, r.y, _width, r.height + 2 );
			_button.buttonMode = true;
			_button.addEventListener( MouseEvent.MOUSE_OVER, mouseHandler );
			_button.addEventListener( MouseEvent.MOUSE_OUT, mouseHandler );
			addChildAt( _button, 0 );
			
			_itemgraphic = new Sprite( );
			_itemgraphic.graphics.clear( );
			_itemgraphic.graphics.beginFill( 0xFF0000 );
			_itemgraphic.graphics.drawRoundRect( 0, 0, 14, r.height - 6, 8 );
			_itemgraphic.graphics.endFill( );
			_itemgraphic.x = _width - 17;
			_itemgraphic.y = 4;
			_itemgraphic.mouseEnabled = false;
			_itemgraphic.mouseChildren = false;
			addChild( _itemgraphic );

			var nformat : TextFormat = _textfield.getTextFormat( );
			nformat.color = 0xFFFFFF;
			nformat.align = TextFormatAlign.CENTER;
			
			_itemtf = new TextField( );
			_itemtf.autoSize = TextFieldAutoSize.CENTER;
			_itemtf.antiAliasType = AntiAliasType.ADVANCED;
			_itemtf.gridFitType = GridFitType.PIXEL;
			_itemtf.type = TextFieldType.DYNAMIC;
			_itemtf.embedFonts = true;
			_itemtf.selectable = false;
			_itemtf.text = String( _items );
			_itemtf.setTextFormat( nformat );
			_itemtf.defaultTextFormat = nformat;
			_itemtf.multiline = false;
			_itemtf.wordWrap = false;
			_itemtf.width = _width;
			_itemtf.x = 2;
			_itemtf.y = -4;
			_itemtf.mouseEnabled = false;
			_itemgraphic.addChild( _itemtf );
			_itemgraphic.visible = false;
			
			_preloaderbarbelow = new Sprite( );
			_preloaderbarbelow.graphics.beginFill( 0, .15 );
			_preloaderbarbelow.graphics.drawRect( 0, 0, 70, r.height - 6 );
			_preloaderbarbelow.graphics.endFill( );
			_preloaderbarbelow.x = _button.x + _button.width - 90;
			_preloaderbarbelow.y = 4;
			_preloaderbarbelow.mouseEnabled = _preloaderbarbelow.mouseChildren = false;
			_preloaderbarbelow.alpha = 0;
			_preloaderbarbelow.visible = false;
			addChild( _preloaderbarbelow );
			
			_preloaderbar = new Sprite( );
			_preloaderbar.graphics.beginFill( 0, .65 );
			_preloaderbar.graphics.drawRect( 0, 0, 70, r.height - 6 );
			_preloaderbar.graphics.endFill( );
			_preloaderbar.x = _preloaderbarbelow.x;
			_preloaderbar.y = 4;
			_preloaderbar.scaleX = .01;
			_preloaderbar.mouseEnabled = _preloaderbar.mouseChildren = false;
			_preloaderbar.alpha = 0;
			_preloaderbar.visible = false;
			addChild( _preloaderbar );
		}

		
		private function mouseHandler( e : MouseEvent ) : void {
			if( e.type == MouseEvent.MOUSE_OVER ) {
				FilterEffects.addTintOverlay( Sprite( e.currentTarget ), .9 );
			} else if( e.type == MouseEvent.MOUSE_OUT ) {
				FilterEffects.removeTintOverlay( Sprite( e.currentTarget ) );
			}
		}

		
		public function add() : int {
			_itemtf.text = String( ++_items );
			_itemgraphic.visible = true;
			return _items;
		}

		
		public function remove() : int {
			if( _items > 0 ) {
				_itemtf.text = String( --_items );
			}
			if( _items == 0 ) {
				_itemgraphic.visible = false;
			}
			return _items;
		}

		
		public function reset() : void {
			_items = 0;
			_itemtf.text = "0";
			_itemgraphic.visible = false;
		}

		
		public function get data() : ListBoxItemData {
			return _data;
		}

		
		public function set data(data : ListBoxItemData) : void {
			_data = data;
		}

		
		public function get filename() : String {
			return _textfield.text;
		}

		
		
		public function select() : void 
		{
			bitmap = Bitmap( BitmapModifierTool.convertToBitmap( this ) );
			addChild( bitmap );
			bitmap.blendMode = BlendMode.INVERT;
			disable();
		}

		
		
		public function deselect() : void 
		{
			if( bitmap ) {
				removeChild( bitmap );
				bitmap = null;
			}
			enable( );
		}

		
		
		public function disable() : void 
		{
			mouseChildren = mouseEnabled = false;
		}


		public function enable() : void 
		{
			mouseChildren = mouseEnabled = true;
		}
	}
}
